package com.yanqu.road.dao.impl.player;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.grandchild.JoinExamGrandChild;

import java.math.BigInteger;
import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class GrandChildExamJoinDaoImpl extends TempDataObjectDao<JoinExamGrandChild> {

    public static String getJoinExamGrandChildSql = "select * from `t_c_grand_child_exam_join` where `examNo`>=? order by examNo,pos asc";

    public Map<Long, List<JoinExamGrandChild>> getJoinExamGrandChildMap(long minGlobalId) {
        Map<Long, List<JoinExamGrandChild>> map = new ConcurrentHashMap<>();
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getCrossDb());
            call = getDb().getConnection().prepareCall(getJoinExamGrandChildSql);
            int i = 1;
            call.setLong(i++, minGlobalId);
            rs = call.executeQuery();
            while (rs.next()) {
                JoinExamGrandChild parse = parse(rs);
                if (!map.containsKey(parse.getExamNo())) {
                    map.put(parse.getExamNo(), new ArrayList<>());
                }
                map.get(parse.getExamNo()).add(parse);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return map;
    }

    public static String updateJoinExamGrandChildSql = "update `t_c_grand_child_exam_join` set userId=?,nickName=?,earnSpeed=?,appearanceId=?,pos=?,title=?,buff=? where examNo=? and id=?";

    @Override
    protected boolean updateData(JoinExamGrandChild joinExamGrandChild) {
        joinExamGrandChild.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCrossDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.BIGINT, joinExamGrandChild.getUserId()));
        para.put(i++, new DbParameter(Types.VARCHAR, joinExamGrandChild.getNickName()));
        para.put(i++, new DbParameter(Types.VARCHAR, joinExamGrandChild.getEarnSpeed().toString()));
        para.put(i++, new DbParameter(Types.INTEGER, joinExamGrandChild.getAppearanceId()));
        para.put(i++, new DbParameter(Types.INTEGER, joinExamGrandChild.getPos()));
        para.put(i++, new DbParameter(Types.INTEGER, joinExamGrandChild.getTitle()));
        para.put(i++, new DbParameter(Types.VARCHAR, joinExamGrandChild.getBuff()));
        para.put(i++, new DbParameter(Types.BIGINT, joinExamGrandChild.getExamNo()));
        para.put(i++, new DbParameter(Types.VARCHAR, joinExamGrandChild.getId()));
        if (getDb().executeNoneQuery(updateJoinExamGrandChildSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String insertJoinExamGrandChildSql = "insert into `t_c_grand_child_exam_join`(examNo,id,userId,nickName,earnSpeed,appearanceId,pos,title,buff) values (?,?,?,?,?,?,?,?,?)";

    @Override
    protected boolean addData(JoinExamGrandChild joinExamGrandChild) {
        joinExamGrandChild.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCrossDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.BIGINT, joinExamGrandChild.getExamNo()));
        para.put(i++, new DbParameter(Types.VARCHAR, joinExamGrandChild.getId()));
        para.put(i++, new DbParameter(Types.BIGINT, joinExamGrandChild.getUserId()));
        para.put(i++, new DbParameter(Types.VARCHAR, joinExamGrandChild.getNickName()));
        para.put(i++, new DbParameter(Types.VARCHAR, joinExamGrandChild.getEarnSpeed().toString()));
        para.put(i++, new DbParameter(Types.INTEGER, joinExamGrandChild.getAppearanceId()));
        para.put(i++, new DbParameter(Types.INTEGER, joinExamGrandChild.getPos()));
        para.put(i++, new DbParameter(Types.INTEGER, joinExamGrandChild.getTitle()));
        para.put(i++, new DbParameter(Types.VARCHAR, joinExamGrandChild.getBuff()));
        if (getDb().executeNoneQuery(insertJoinExamGrandChildSql, para) > 0) {
            return true;
        }
        return false;
    }

    @Override
    protected JoinExamGrandChild parseData(ResultSet rs) throws SQLException {
        JoinExamGrandChild joinExamGrandChild = new JoinExamGrandChild();
        joinExamGrandChild.setExamNo(rs.getLong("examNo"));
        joinExamGrandChild.setId(rs.getString("id"));
        joinExamGrandChild.setUserId(rs.getLong("userId"));
        joinExamGrandChild.setNickName(rs.getString("nickName"));
        joinExamGrandChild.setEarnSpeed(new BigInteger(rs.getString("earnSpeed")));
        joinExamGrandChild.setAppearanceId(rs.getInt("appearanceId"));
        joinExamGrandChild.setPos(rs.getInt("pos"));
        joinExamGrandChild.setTitle(rs.getInt("title"));
        joinExamGrandChild.setBuff(rs.getString("buff"));
        return joinExamGrandChild;
    }

}
